import 'package:flutter/cupertino.dart';
import 'package:macos_ui/macos_ui.dart';
import '../models/mcp_tool.dart';

/// 工具列表组件
class ToolList extends StatelessWidget {
  final List<McpTool> tools;
  final McpTool? selectedTool;
  final ValueChanged<McpTool> onToolSelected;
  final bool isLoading;

  const ToolList({
    super.key,
    required this.tools,
    this.selectedTool,
    required this.onToolSelected,
    this.isLoading = false,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 标题栏
        Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            color: MacosTheme.of(context).canvasColor,
            border: Border(
              bottom: BorderSide(
                color: MacosTheme.of(context).dividerColor,
              ),
            ),
          ),
          child: Row(
            children: [
              Icon(
                CupertinoIcons.square_list,
                size: 20,
                color: MacosTheme.of(context).typography.body.color,
              ),
              const SizedBox(width: 8),
              Text(
                '工具列表 (${tools.length})',
                style: MacosTheme.of(context).typography.headline.copyWith(
                  fontSize: 16,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
        // 工具列表内容
        Expanded(
          child: _buildContent(context),
        ),
      ],
    );
  }

  Widget _buildContent(BuildContext context) {
    if (isLoading) {
      return const Center(child: ProgressCircle());
    }

    if (tools.isEmpty) {
      return Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              CupertinoIcons.square_list,
              size: 48,
              color: MacosTheme.of(
                context,
              ).typography.body.color?.withOpacity(0.3),
            ),
            const SizedBox(height: 16),
            Text(
              '没有可用的工具',
              style: MacosTheme.of(context).typography.body.copyWith(
                color: MacosTheme.of(
                  context,
                ).typography.body.color?.withOpacity(0.5),
              ),
            ),
            const SizedBox(height: 8),
            Text(
              '请先配置 MCP 连接',
              style: MacosTheme.of(context).typography.caption1.copyWith(
                color: MacosTheme.of(
                  context,
                ).typography.body.color?.withOpacity(0.5),
              ),
            ),
          ],
        ),
      );
    }

    return ListView.builder(
      itemCount: tools.length,
      itemBuilder: (context, index) {
        final tool = tools[index];
        final isSelected = selectedTool?.name == tool.name;

        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
          child: GestureDetector(
            onTap: () => onToolSelected(tool),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Icon(
                  CupertinoIcons.wrench_fill,
                  color: isSelected
                      ? MacosTheme.of(context).primaryColor
                      : MacosTheme.of(
                          context,
                        ).typography.body.color?.withOpacity(0.6),
                  size: 20,
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: Container(
                    decoration: BoxDecoration(
                      color: isSelected
                          ? MacosTheme.of(context).primaryColor.withOpacity(0.15)
                          : MacosColors.transparent,
                      borderRadius: BorderRadius.circular(6),
                    ),
                    padding: const EdgeInsets.symmetric(
                      horizontal: 12,
                      vertical: 8,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          tool.name,
                          style: MacosTheme.of(context).typography.body.copyWith(
                            fontWeight: isSelected
                                ? FontWeight.w600
                                : FontWeight.w500,
                            color: isSelected
                                ? MacosTheme.of(context).primaryColor
                                : null,
                          ),
                        ),
                        if (tool.description.isNotEmpty) ...[
                          const SizedBox(height: 4),
                          Text(
                            tool.description,
                            style: MacosTheme.of(context).typography.caption1
                                .copyWith(
                                  color: MacosTheme.of(
                                    context,
                                  ).typography.body.color?.withOpacity(0.6),
                                ),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                        if (tool.parameters.isNotEmpty) ...[
                          const SizedBox(height: 6),
                          Row(
                            children: [
                              Icon(
                                CupertinoIcons.doc_text,
                                size: 12,
                                color: MacosTheme.of(
                                  context,
                                ).typography.body.color?.withOpacity(0.4),
                              ),
                              const SizedBox(width: 4),
                              Text(
                                '${tool.parameters.length} 个参数',
                                style: MacosTheme.of(context).typography.caption2
                                    .copyWith(
                                      color: MacosTheme.of(
                                        context,
                                      ).typography.body.color?.withOpacity(0.5),
                                    ),
                              ),
                            ],
                          ),
                        ],
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
